<!--
@license
Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-dialog/paper-dialog.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../tf-backend/tf-backend.html">
<link rel="import" href="../tf-color-scale/tf-color-scale.html">
<link rel="import" href="../tf-dashboard-common/tf-dashboard.html">
<link rel="import" href="../tf-dashboard-common/tf-panes-helper.html">
<link rel="import" href="../tf-dashboard-common/tf-sidebar-helper.html">
<link rel="import" href="tf-image-loader.html">

<!--
tf-image-dashboard displays a dashboard that loads images from a TensorFlow run.
-->
<dom-module id="tf-image-dashboard">
  <template>
    <paper-dialog with-backdrop id="actual-image-size-dialog"></paper-dialog>
    <div id="plumbing">
      <tf-color-scale
        id="colorScale"
        runs="[[runs]]"
        out-color-scale="{{_colorScale}}"
        ></tf-color-scale>
    </div>

    <tf-dashboard-layout>
      <div class="sidebar">
        <tf-sidebar-helper
          backend="[[backend]]"
          categories="{{_categories}}"
          color-scale="[[_colorScale]]"
          run2tag="[[run2tag]]"
          runs="[[runs]]"
          selected-runs="{{_selectedRuns}}"
          >
        </tf-sidebar-helper>
      </div>
      <div class="center">
        <tf-panes-helper
          categories="[[_categories]]"
          color-scale="[[_colorScale]]"
          data-type="[[dataType]]"
          data-provider="[[dataProvider]]"
          run2tag="[[run2tag]]"
          selected-runs="[[_selectedRuns]]"
          repeat-for-runs
          >
          <template>
            <tf-image-loader color-scale="[[_colorScale]]"></tf-image-loader>
            <paper-icon-button
              class="actual-size-button"
              icon="aspect-ratio"
              on-tap="_showActualSize"
              title="Show the image at its true pixel size"
              ></paper-icon-button>
          </template>
        </tf-panes-helper>
      </div>
    </tf-dashboard-layout>
    <style include="dashboard-style"></style>
    <style>
      tf-panes-helper {
        --card-width: 340px;
        --card-height: auto;
        --card-expanded-width: 700px;
        --card-expanded-height: auto;
      }

      .actual-size-button {
        background: #fff;
        border-radius: 100%;
        bottom: -35px;
        color: #2196f3;
        height: 32px;
        left: 35px;
        padding: 4px;
        pointer-events: auto;
        position: absolute;
        width: 32px;
      }

      .actual-size-button-selected {
        background: var(--tb-ui-light-accent);
      }

      #actual-image-size-dialog {
        overflow: auto;
      }
    </style>
  </template>
  <script>
    Polymer({
      is: "tf-image-dashboard",
      properties: {
        backend: Object,
        dataType: {
          type: String,
          value: "image"
        },
      },
      behaviors: [
          TF.Dashboard.ReloadBehavior("tf-chart-scaffold"),
          TF.Backend.Behavior,
      ],
      attached: function() {
        this.async(function() {
          this.fire("rendered");
        });
      },
      _showActualSize: function(e) {
        var currentTarget = Polymer.dom(e.currentTarget);
        var card = currentTarget.node.closest('.card');

        // Create a full-size copy of the image.
        var newImage = card.querySelector('#img').cloneNode();
        newImage.style.height = 'auto';
        newImage.style.width = 'auto';
        newImage.style.margin = 0;
        newImage.style.padding = 0;
        newImage.classList.add("actual-size-image");

        // When the user clicks on the image, empty and close the dialog.
        var dialog = this.$$('#actual-image-size-dialog');
        newImage.addEventListener('click', function() {
          dialog.close();
        });

        // Update dialog content. Show the dialog.
        dialog.innerHTML = '';
        dialog.appendChild(newImage);
        dialog.open();
      }
    });
  </script>
</dom-module>
